/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jastrzab.core;

import android.graphics.Rect;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import java.io.Serializable;
import java.util.List;

/**
 *
 * @author Jastrzab
 */
public final class PreviewFormat implements Serializable {

	private static final long serialVersionUID = 1348975023984398340L;
	public int mWidth = 0;
	public int mHeight = 0;

	public PreviewFormat() {
		this(0, 0);
	}

	public PreviewFormat(PreviewFormat format) {
		this(format.mWidth, format.mHeight);
	}

	public PreviewFormat(int width, int height) {
		mWidth = width;
		mHeight = height;
	}

	public void setFormat(int width, int height) {
		mWidth = width;
		mHeight = height;
	}

	public Rect getRect() {
		return new Rect(0, 0, mWidth, mHeight);
	}

	public static PreviewFormat getMaxSize(Parameters parameters) {
		if (parameters != null) {
			Size currentSize = parameters.getPreviewSize();
			PreviewFormat newSize = new PreviewFormat(currentSize.width, currentSize.height);
			List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
			if (supportedPreviewSizes != null) {
				for (Size size : supportedPreviewSizes) {
					newSize.setFormat(Math.max(currentSize.width, size.width), Math.max(currentSize.height, size.height));
				}
			}
			return newSize;
		}
		return null;
	}

	public static PreviewFormat getMinSize(Parameters parameters) {
		if (parameters != null) {
			Size currentSize = parameters.getPreviewSize();
			PreviewFormat newSize = new PreviewFormat(currentSize.width, currentSize.height);
			List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
			if (supportedPreviewSizes != null) {
				for (Size size : supportedPreviewSizes) {
					newSize.setFormat(Math.min(currentSize.width, size.width), Math.min(currentSize.height, size.height));
				}
			}
			return newSize;
		}
		return null;
	}

}
